<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
	.green{
	 color:green
	}
	.red{
	 color:red
	}
	
	</style>
  <script type="text/javascript">
   window.onload=function(){
	   var xhr=null;
	   //获取核心对象的方法
	   function getXhr(){
		   if (window.XMLHttpRequest) {
			xhr=new XMLHttpRequest;
	       }else if(window.ActiveObject){
	    	   xhr=new ActiveObject("Microsoft.XMLHttp");
	       }else{
	    	  alert("不行!!");
	       }
	   }
	   
	   btn.onclick=function(){
		 //01.获取核心对象
		  getXhr();
		 //02.回调函数
		 xhr.onreadystatechange=function(){
			 if (xhr.readyState==4&&xhr.status==200) {
				if (xhr.responseText.match(/ok/)) {
					alert(xhr.responseText);
					//登陆成功
					myDiv.innerHTML="<span class='green'>2秒后跳转到主界面!!</span>";
					setTimeout(function(){
						location.href="success.jsp";
					},2000)
				}else if(xhr.responseText.match(/error/)){
					myDiv.innerHTML="<span class='red'>您输入有误!!</span>";
					alert(xhr.responseText);
				}
			}
		 }
		 //get
		/*  xhr.open("get","check.jsp?username="+username.value+"&pwd="+pwd.value,true);
		    xhr.send(); */
		
		 //post
		 xhr.open("post", "j.jsp", true);
		 xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		 xhr.send("username="+username.value+"&pwd="+pwd.value);
		}
	   
	  
	   
	   
   }
  
  
  </script>




  </head>
  
  <body>
<input type="text" name="username" id="username"/><br><br>
<input type="password" name="pwd" id="pwd"/><br>
<input type="submit" value="登陆" id="btn">

<div id="myDiv"></div>
  </body>
</html>
  
